package com.zxcl.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zxcl.domain.Basket;
import com.zxcl.model.Result;
import com.zxcl.service.BasketService;
import com.zxcl.vo.CartMoneyVO;
import com.zxcl.vo.CartVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@Api(tags = "购物车管理接口")
@RequestMapping("p/shopCart")
public class BasketController {

    @Autowired
    private BasketService basketService;

    @GetMapping("prodCount")
    @ApiOperation("查询购物车数量")
    public Result<Integer> basketCount() {
        Integer count = basketService.basketCount();
        return Result.success(count);
    }


    /**
     * 添加或者修改购物车数量
     */
    @PostMapping("changeItem")
    @ApiOperation("添加或者修改购物车数量")
    public Result<String> changeBasket(@RequestBody @Validated Basket basket) {
        Integer i = basketService.changeBasket(basket);
        return Result.handle(i > 0);
    }
    /**
     * 购物车详情
     * @return
     */
    @GetMapping("info")
    @ApiOperation("购物车详情")
    public Result<CartVO> cartInfo() {
        CartVO cartVO = basketService.cartInfo();
        return Result.success(cartVO);
    }


    /**
     * 计算购物车选中商品的价格
     * @param basketIds
     * @return
     */
    @PostMapping("totalPay")
    @ApiOperation("计算购物车选中商品的价格")
    public Result<CartMoneyVO> calculateBasketMoney(@RequestBody List<Long> basketIds) {
        CartMoneyVO cartMoneyVO = basketService.calculateBasketMoney(basketIds);
        return Result.success(cartMoneyVO);
    }


    /*************远程调用*************/
    /**
     * 根据ids查询购物车集合
     * @param basketIds
     * @return
     */
    @GetMapping("getBasketsByIds")
    @ApiOperation("根据ids查询购物车集合")
    Result<List<Basket>> getBasketsByIds(@RequestParam("basketIds") List<Long> basketIds) {
        List<Basket> basketList = basketService.list(new LambdaQueryWrapper<Basket>()
                .in(Basket::getBasketId, basketIds)
                .orderByDesc(Basket::getCreateTime)
        );
        return Result.success(basketList);
    }

    /**
     * 根据会员openId和skuIds清空购物车
     */
    @PostMapping("clearCart")
    @ApiOperation("根据会员openId和skuIds清空购物车")
    Result<Boolean> clearCart(@RequestBody List<Long> skuIds, @RequestParam("openId") String openId){
        boolean flag = basketService.remove(new LambdaQueryWrapper<Basket>()
                .eq(Basket::getOpenId, openId)
                .in(Basket::getSkuId, skuIds)
        );
        return Result.success(flag);
    }





}
